

#include <stdint.h>
#include "sensorsTask.h"
#include "FreeRTOS.h"
#include "task.h"

#include "nxbot.h"
#include "sonar.h"

extern xTaskHandle soundHandle;

static void sensorsTask(void *pvParameters);


void executeSensorsTask(globals_t *pvParameters)
{

	xTaskCreate(sensorsTask,(const signed portCHAR * const)"iRSnTask",sensorsTask_stackSize,(void *)pvParameters,tskIDLE_PRIORITY + 2,( xTaskHandle * ) NULL);
}

//tarea que solo actualiza la info de los sensores IR
//TODO: polling de bumpers, y de bateria????
static void sensorsTask(void *pvParameters)
{	
	
	//convertion of parameters 
	globals_t *globals;
	globals=(globals_t *)pvParameters;
	
	//delay
	unsigned int freq;
	portTickType actualTick;

	for(;;)
	{
		//obtain the frequency of the IRSensors update
		//taskENTER_CRITICAL();
		freq=globals->times.intervalSensors;
		//taskEXIT_CRITICAL();

		sensors_readAll(&(globals->sensors));
		
		//check if battery is below some threshold in order to generate beeps.
		if(globals->sensors.batt.battVoltage <= NXBOT_BATT_ALARM)
		{
			//Play jingle of "battery low". Each 10 seconds the beep will be heared.
			globals->soundJingle = 2;
			vTaskResume( soundHandle );
		}
		
		//time delay according to the irSensorsInterval value
		actualTick = xTaskGetTickCount();			
		vTaskDelayUntil( &actualTick,(portTickType)freq);	



	}
}
